package com.software.notification.menu;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupMenu;
import android.widget.Toast;

import com.software.notification.R;
import com.software.notification.utils.ToastUtil;

public class MenuActivity extends AppCompatActivity {

    private EditText edt_copy;
    private EditText edt_paste;
    private Button btn_popupMenu;
    private final String TAG = "MenuActivity";
    private Button btn_test;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);
        initViews();

        //注册上下文菜单
        registerForContextMenu(edt_copy);
        registerForContextMenu(edt_paste);

        //给按钮绑定事件
        btn_popupMenu.setOnClickListener(v -> {
            //创建弹出式菜单
            PopupMenu  popupMenu = new PopupMenu(MenuActivity.this, btn_popupMenu);
            popupMenu.getMenuInflater().inflate(R.menu.menu_item, popupMenu.getMenu());
            //绑定菜单事件
            popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    ToastUtil.show(MenuActivity.this, item.getTitle().toString());
                    return false;
                }
            });
            //显示菜单
            popupMenu.show();
        });

        btn_test.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                Log.i(TAG, "onLongClick: ");
                return false;
            }
        });
        btn_test.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action = event.getAction();
                if (action == MotionEvent.ACTION_UP){
                    Log.i(TAG, "触摸上");
                }
                if (action == MotionEvent.ACTION_DOWN){
                    Log.i(TAG, "失去触摸");
                }
                Log.i(TAG, "onTouch: ");
                return false;
            }
        });


    }

    private void initViews() {
        edt_copy = findViewById(R.id.edt_copy);
        edt_paste = findViewById(R.id.edt_paste);
        btn_popupMenu = findViewById(R.id.btn_popupMenu);

        btn_test = findViewById(R.id.btn_test);
    }

    //选项菜单
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //关联资源
        getMenuInflater().inflate(R.menu.menu_item, menu);
        return super.onCreateOptionsMenu(menu);
    }

    //选项菜单点击事件
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        ToastUtil.show(this, item.getTitle().toString());
        return super.onOptionsItemSelected(item);
    }

    //上下文菜单

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        getMenuInflater().inflate(R.menu.menu_item, menu);
        super.onCreateContextMenu(menu, v, menuInfo);

    }

    @Override
    public boolean onContextItemSelected(@NonNull MenuItem item) {
        int itemId = item.getItemId();
        //获取系统的剪切板
        ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        //选择复制菜单
        if (itemId == R.id.file_copy){
            //复制
            cm.setPrimaryClip(ClipData.newPlainText("msg", edt_copy.getText()));
            ToastUtil.show(MenuActivity.this, "复制成功");

        } else if (itemId == R.id.file_paste) {
            //粘贴
            edt_paste.setText(cm.getPrimaryClip().getItemAt(0).getText());
        }

        return super.onContextItemSelected(item);
    }

    public void handleClick(View view) {
        Log.i(TAG, "handleClick: ");

    }
}